package com.example.design_pattern.observer.javajdk;


import com.example.design_pattern.observer.DisplayElement;

import java.util.Observable;
import java.util.Observer;

/**
 * Created by SQ_BXZ on 2019-02-16.
 *  布告板 - 当前状况
 */
public class CurrentConditionsDisplay implements Observer, DisplayElement {

    private float temperature;
    private float humidity;
    private Observable observable;

    public CurrentConditionsDisplay(Observable observable) {
        this.observable = observable;
        this.observable.addObserver(this);
    }

    public void display() {
        System.out.println("Current conditions: " + temperature
                + "F degrees and " + humidity + "% humidity");
    }

    public void update(Observable o, Object arg) {
        if (o instanceof WeatherData){
            WeatherData weatherData = ((WeatherData) o);
            this.temperature = weatherData.getTemperature();
            this.humidity = weatherData.getHumidity();
            display();
        }
    }

}
